package com.example.demo;
import java.util.logging.LogManager;

import javax.security.auth.login.LoginException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.media.MediaPlayer;
import android.support.v4.util.LogWriter;
import android.util.AttributeSet;
import android.util.Log;
import android.util.LogPrinter;
import android.view.MotionEvent;
import android.view.View;

public class Lever1 extends View {
  private Paint paint = new Paint();
  Bitmap image;
  int x;
  int y;
  int c_min;
  int c_max;
  int r_min;
  int r_max;
  float x1;
  float x2;
  float y1;
  float y2;
  int x_1;
  int y_1;
  int x_attack;
  int y_attack;
  int eval_attack;
  boolean runing;
  boolean runing_;
  Bitmap win_lose;
  Bitmap back;
  Bitmap audio_on;
  //Bitmap audio_off;
  Boolean audio;
  Bitmap danhlai;
 // MediaPlayer audio_back_ground=null;
  MediaPlayer audio_x;
  MediaPlayer audio_o;
  MediaPlayer audio_lose;
  MediaPlayer audio_win;
  private Bitmap[][] quan_game=new Bitmap[100][];
  private int[][] select_game=new int[100][];
  Context context;
  public Lever1(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
    x_attack=-1;
    y_attack=-1;
    eval_attack=0;
    runing=true;
    runing_=true;
    audio=false;
    x_1=-1;
    y_1=-1;
  //  audio_back_ground=new MediaPlayer();
///    audio_back_ground=MediaPlayer.create(context, R.raw.game_background);
 //   audio_back_ground.setLooping(true);
    //audio_back_ground.start();
    audio_lose=new MediaPlayer();
    audio_lose=MediaPlayer.create(context, R.raw.lose);
    audio_o=new MediaPlayer();
    audio_o=MediaPlayer.create(context, R.raw.draw_o);
    audio_x=new MediaPlayer();
    audio_x=MediaPlayer.create(context, R.raw.draw_x);
    audio_x.setLooping(false);
    audio_win=new MediaPlayer();
    audio_win=MediaPlayer.create(context, R.raw.win);
    audio_win.setLooping(false);
    audio_lose.setLooping(false);
    audio_o.setLooping(false);
//    mp.start();
    //mp.set(Lever1.this,R.raw.game_background);
 for(int i=0;i<100;i++){
	 quan_game[i]=new Bitmap[100];
	 select_game[i]=new int[100];
	 }
 c_min=0;
 c_max=13;
 r_min=0;
 r_max=20;
 back=BitmapFactory.decodeResource(getResources(), R.drawable.back);
 audio_on=BitmapFactory.decodeResource(getResources(), R.drawable.sound_off);
 danhlai=BitmapFactory.decodeResource(getResources(), R.drawable.undo_button_onl_1);
    for(int i=c_min;i<c_max;i++)
	   for(int j=r_min;j<r_max;j++){
    quan_game[i][j]=BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_emty);
	   select_game[i][j]=0;
	   }
   
    
  }
  public void danh(){
	  for(int i=c_min;i<c_max;i++)
		  for(int j=r_min;j<r_max;j++)
			  if(select_game[i][j]==0){
				  select_game[i][j]=1;
				  int m=colum_computing(1)+row_computing(1)+cross_computing_r(1)+computing_cheo_r(1)+eval_chan(i, j)+eval_attack_(i, j);
				  if(eval_attack<m){
					  
					  eval_attack=m;
					  x_attack=i;
					  y_attack=j;
				  }
				  select_game[i][j]=0;
			  }
	  System.out.println(" Tai i="+x_attack+" va j="+y_attack+" thi eval="+eval_attack+"");
	  eval_attack=0;
  }
  public int colum_computing(int k){
	  int sum=0;
	  
	  for(int i=c_min;i<c_max;i++)
	  {
		  int d=1;
		  for(int j=r_min;j<r_max;j++){
			  if(select_game[i][j]==k){
				  for(int j1=j+1;j1<r_max;j1++)
					  if(select_game[i][j1]==k){
						  d++;
						  j=j1+1;
					  }
					  else{
						  sum+=d*d;
						  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
						  if(d==4)
							  sum+=50;
						  else if(d==5)
							  sum+=100;
						  d=1;
						  j=j1;
						  break;
					  }
			  }
		  }
	  }
	  return sum;
  }
  public int row_computing(int k){
	  int sum=0;
	  for(int i=r_min;i<r_max;i++)
	  {
		  int d=1;
		  for(int j=c_min;j<c_max;j++){
			  if(select_game[j][i]==k){
				  for(int j1=j+1;j1<c_max;j1++)
					  if(select_game[i+j1-j][j1]==k){
						  d++;
						  j=j1+1;
					  }
					  else{
						  sum+=d*d;
						  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
						  if(d==4)
							  sum+=50;
						  else if(d==5)
							  sum+=100;
						  d=1;
						  j=j1;
						  break;
					  }
			  }
		  }
	  }
	  return sum;
	  
  }
  public int cross_computing_r(int k){
	  int sum=0;
	  int d=1;
	 for(int i=c_min;i<c_max;i++)
		 for(int j=i;j>=0;j--)
		 
			 if(select_game[i-j][j]==k)
			 {
				 d=1;
				 for(int j1=j-1;j1>=0;j1--)
					 if(select_game[i-j1][j1]==k){
						 d++;
						 j=j1-1;
					 }else
					 {
						 sum+=d*d;
						  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
						  if(d==4)
							  sum+=50;
						  else if(d==5)
							  sum+=100;
						 j=j1;
						 d=0;
						 break;
						 
					 }
				 sum+=d*d;
			 }
	 for(int j=c_max;j<r_max;j++)
		 for(int i=c_min;i<c_max;i++)
			 if(select_game[i][j]==k){
				 d=1;
				 for(int i1=i+1;i1<c_max;i1++)
					 if(select_game[i1][j-(i1-i)]==k){
						 d++;
						 i=i1+1;
					 }else 
					 {
						 sum+=d*d;
						 if(d==4)
							 sum+=50;
						 else if(d==5)
							 sum+=100;
						 i=i1;
						 d=0;
						 break;
					 }
				 sum+=d*d;
			 }
			 
		 
	  return sum;
  }
  public int computing_cheo_r(int k){
	  int sum=0;
	  int d=1;
	  for(int i=c_min;i<c_max;i++)
		  for(int j=r_min;j<c_max-i;j++){
			  if(select_game[i+j][j]==k)
			  {
				  d=1;
				  for(int j1=j+1;j1<c_max-i;j1++)
					  if(select_game[i+j1][j1]==k){
						  d++;
						  j=j1+1;
						  
					  }else{
						  sum+=d*d;
						  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
						  if(d==4)
							  sum+=50;
						  else if(d==5)
							  sum+=100;
						  d=0;
						  j=j1;
						  break;
					
					  }
				  sum+=d*d;
			  }
		  }
		for(int j=c_max;j<r_max;j++)
			for(int i=c_max-1;i>=c_min;i--)
				if(select_game[i][j]==k){
					d=1;
					for(int i1=i-1;i1>=c_min;i1--)
						if(select_game[i1][j-(i-i1)]==k){
							d++;
							i=i1-1;
						}else{
							sum+=d*d;
						if(d==4)
							sum+=40;
						else if(d==5)
							sum+=100;
						d=0;
						i=i1;
						break;
						}
					sum+=d*d;
					
				}
		  
	  return sum;
  }
  public int eval_attack_(int i,int j){
	  int sum=0;
	  if(i>=4&&select_game[i-1][j]==-1){
		  if(select_game[i-2][j]==1)
			  if(select_game[i-3][j]==1)
				  if(select_game[i-4][j]==1)
					  sum+=150;
				  else{
					  if(select_game[i-4][j]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  }
	  if(j>=4&&select_game[i][j-1]==1)
		  if(select_game[i][j-2]==1)
			  if(select_game[i][j-3]==1)
				  if(select_game[i][j-4]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i][j-4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i<c_max-4&&select_game[i+1][j]==1)
		  if(select_game[i+2][j]==1)
			  if(select_game[i+3][j]==1)
				  if(select_game[i+4][j]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i+4][j]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(j<r_max-4&&select_game[i][j+1]==1)
		  if(select_game[i][j+2]==1)
			  if(select_game[i][j+3]==1)
				  if(select_game[i][j+4]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i][j+4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  
	  
	  if(i>=4&&j>=4&&select_game[i-1][j-1]==1)
		  if(select_game[i-2][j-2]==1)
			  if(select_game[i-3][j-3]==1)
				  if(select_game[i-4][j-4]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i-4][j-4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i<c_max-4&&j<r_max-4&&select_game[i+1][j+1]==1)
		  if(select_game[i+2][j+2]==1)
			  if(select_game[i+3][j+3]==1)
				  if(select_game[i+4][j+4]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i+4][j+4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i>=4&&j<r_max-4&&select_game[i-1][j+1]==1)
		  if(select_game[i-2][j+2]==1)
			  if(select_game[i-3][j+3]==1)
				  if(select_game[i-4][j+4]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i-4][j+4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i<c_max-4&&j>=4&&select_game[i+1][j-1]==1)
		  if(select_game[i+2][j-2]==1)
			  if(select_game[i+3][j-3]==1)
				  if(select_game[i+4][j-4]==1)
					  sum+=150;
				  else
				  {
					  if(select_game[i+4][j-4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  // chặn 3 nguy hiểm  xx x 
	  if(i>=3&&i<c_max-2&&select_game[i-2][j]==1&&select_game[i-1][j]==1&&select_game[i+1][j]==1&&select_game[i-3][j]!=-1&&select_game[i+2][j]!=-1)
		  sum+=150;
	  //chan 3 nguy hiem x xx
if(i>=2&&i<c_max-3&&select_game[i-1][j]==1&&select_game[i-2][j]!=-1&&select_game[i+1][j]==1&&select_game[i+2][j]==1&&select_game[i+3][j]!=-1)
	sum+=150;
if(j>=3&&j<r_max-2&&select_game[i][j-2]==1&&select_game[i][j-1]==1&&select_game[i][j+1]==1&&select_game[i][j-3]!=-1&&select_game[i][j+2]!=-1)
	  sum+=150;
//chan 3 nguy hiem x xx
if(j>=2&&j<r_max-3&&select_game[i][j-1]==1&&select_game[i][j-2]!=-1&&select_game[i][j+1]==1&&select_game[i][j+2]==1&&select_game[i][j+3]!=-1)
sum+=150;
if(i>=3&&j>=3&&j<r_max-2&&i<c_max-2&&select_game[i-3][j-3]!=-1&&select_game[i-2][j-2]==1&&select_game[i-1][j-1]==1&&select_game[i+1][j+1]==1&&select_game[i+2][j+2]!=-1)
	sum+=150;
if(i>=2&&j>=2&&i<c_max-3&&j<r_max-3&&select_game[i-2][j-2]!=-1&&select_game[i-1][j-1]==1&&select_game[i+1][j+1]==1&select_game[i+2][j+2]==1&&select_game[i+3][j+3]!=-1)
	sum+=150;
if(i>=3&&i<c_max-2&&j<r_max-3&&j>=2&&select_game[i-3][j+3]!=-1&&select_game[i-2][j+2]==1&&select_game[i-1][j+1]==1&&select_game[i+1][j-1]==1&&select_game[i+2][j-2]!=-1)
	sum+=150;
if(i>=2&&i<c_max-3&&j<r_max-2&&j>=3&&select_game[i-2][j+2]!=-1&&select_game[i-1][j+1]==1&&select_game[i+1][j-1]==1&&select_game[i+2][j-2]==1&&select_game[i+3][j-3]!=-1)
	sum+=150;
//Tan cong 3 nguy hiem
if(i<c_max-4&&select_game[i+1][j]==1&&select_game[i+2][j]==1&&select_game[i+3][j]==1&&select_game[i+4][j]!=-1)
	sum+=150;
if(j<r_max-4&&select_game[i][j+1]==1&&select_game[i][j+2]==1&&select_game[i][j+3]==1&&select_game[i][j+4]!=-1)
	sum+=150;
if(i<c_max-4&&j<r_max-4&&select_game[i+1][j+1]==1&&select_game[i+2][j+2]==1&&select_game[i+3][j+3]==1&&select_game[i+4][j+4]!=-1)
	sum+=150;
if(i<c_max-4&&j>=4&&select_game[i+1][j-1]==1&&select_game[i+2][j-2]==1&&select_game[i+3][j-3]==1&&select_game[i+4][j-4]!=-1)
	sum+=150;
	  return sum;
  }
  public int eval_chan(int i,int j){
	  int sum=0;
	  if(i>=4&&select_game[i-1][j]==-1){
		  if(select_game[i-2][j]==-1)
			  if(select_game[i-3][j]==-1)
				  if(select_game[i-4][j]==-1)
					  sum+=150;
				  else{
					  if(select_game[i-4][j]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  }
	  if(j>=4&&select_game[i][j-1]==-1)
		  if(select_game[i][j-2]==-1)
			  if(select_game[i][j-3]==-1)
				  if(select_game[i][j-4]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i][j-4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i<c_max-4&&select_game[i+1][j]==-1)
		  if(select_game[i+2][j]==-1)
			  if(select_game[i+3][j]==-1)
				  if(select_game[i+4][j]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i+4][j]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(j<r_max-4&&select_game[i][j+1]==-1)
		  if(select_game[i][j+2]==-1)
			  if(select_game[i][j+3]==-1)
				  if(select_game[i][j+4]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i][j+4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  
	  
	  if(i>=4&&j>=4&&select_game[i-1][j-1]==-1)
		  if(select_game[i-2][j-2]==-1)
			  if(select_game[i-3][j-3]==-1)
				  if(select_game[i-4][j-4]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i-4][j-4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i<c_max-4&&j<r_max-4&&select_game[i+1][j+1]==-1)
		  if(select_game[i+2][j+2]==-1)
			  if(select_game[i+3][j+3]==-1)
				  if(select_game[i+4][j+4]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i+4][j+4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i>=4&&j<r_max-4&&select_game[i-1][j+1]==-1)
		  if(select_game[i-2][j+2]==-1)
			  if(select_game[i-3][j+3]==-1)
				  if(select_game[i-4][j+4]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i-4][j+4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  if(i<c_max-4&&j>=4&&select_game[i+1][j-1]==-1)
		  if(select_game[i+2][j-2]==-1)
			  if(select_game[i+3][j-3]==-1)
				  if(select_game[i+4][j-4]==-1)
					  sum+=150;
				  else
				  {
					  if(select_game[i+4][j-4]==0)
						  sum+=50;
					  else
					  sum+=3*3;
				  }
			  else
				  sum+=2*2;
		  else
			  sum+=1*1;
	  // chặn 3 nguy hiểm  xx x 
	  if(i>=3&&i<c_max-2&&select_game[i-2][j]==-1&&select_game[i-1][j]==-1&&select_game[i+1][j]==-1&&select_game[i-3][j]!=1&&select_game[i+2][j]!=1)
		  sum+=150;
	  //chan 3 nguy hiem x xx
if(i>=2&&i<c_max-3&&select_game[i-1][j]==-1&&select_game[i-2][j]!=1&&select_game[i+1][j]==-1&&select_game[i+2][j]==-1&&select_game[i+3][j]!=1)
	sum+=150;
if(j>=3&&j<r_max-2&&select_game[i][j-2]==-1&&select_game[i][j-1]==-1&&select_game[i][j+1]==-1&&select_game[i][j-3]!=1&&select_game[i][j+2]!=1)
	  sum+=150;
//chan 3 nguy hiem x xx
if(j>=2&&j<r_max-3&&select_game[i][j-1]==-1&&select_game[i][j-2]!=1&&select_game[i][j+1]==-1&&select_game[i][j+2]==-1&&select_game[i][j+3]!=1)
sum+=150;
if(i>=3&&j>=3&&j<r_max-2&&i<c_max-2&&select_game[i-3][j-3]!=1&&select_game[i-2][j-2]==-1&&select_game[i-1][j-1]==-1&&select_game[i+1][j+1]==-1&&select_game[i+2][j+2]!=1)
	sum+=150;
if(i>=2&&j>=2&&i<c_max-3&&j<r_max-3&&select_game[i-2][j-2]!=1&&select_game[i-1][j-1]==-1&&select_game[i+1][j+1]==-1&select_game[i+2][j+2]==-1&&select_game[i+3][j+3]!=1)
	sum+=150;
if(i>=3&&i<c_max-2&&j<r_max-3&&j>=2&&select_game[i-3][j+3]!=1&&select_game[i-2][j+2]==-1&&select_game[i-1][j+1]==-1&&select_game[i+1][j-1]==-1&&select_game[i+2][j-2]!=1)
	sum+=150;
if(i>=2&&i<c_max-3&&j<r_max-2&&j>=3&&select_game[i-2][j+2]!=1&&select_game[i-1][j+1]==-1&&select_game[i+1][j-1]==-1&&select_game[i+2][j-2]==-1&&select_game[i+3][j-3]!=1)
	sum+=150;
//chặn 3 nguy hiểm xxx ,ngang ,dọc,chéo 
if(i<c_max-4&&select_game[i+1][j]==-1&&select_game[i+2][j]==-1&&select_game[i+3][j]==-1&&select_game[i+4][j]!=1)
	sum+=150;
if(j<r_max-4&&select_game[i][j+1]==-1&&select_game[i][j+2]==-1&&select_game[i][j+3]==-1&&select_game[i][j+4]!=1)
	sum+=150;
if(i<c_max-4&&j<r_max-4&&select_game[i+1][j+1]==-1&&select_game[i+2][j+2]==-1&&select_game[i+3][j+3]==-1&&select_game[i+4][j+4]!=1)
	sum+=150;
if(i<c_max-4&&j>=4&&select_game[i+1][j-1]==-1&&select_game[i+2][j-2]==-1&&select_game[i+3][j-3]==-1&&select_game[i+4][j-4]!=1)
	sum+=150;
	  return sum;
  }
  public boolean check_win(int k){
	  int d=0;
	  int d1=-1;
	  //Kiem tra an ngang
	  for(int j=r_min;j<r_max;j++){
		  
	  d=0;
	  
		  for(int i=c_min;i<c_max;i++)
		  {
			  if(select_game[i][j]==k)
			  {
				  d=1;
				  d1=-1;
				  for(int i1=i+1;i1<c_max;i1++)
					  if(select_game[i1][j]==k){
						  d++;
						  i=i1+1;
						  d1=i1+1;
					  }
					  else {
						  
						  break;
						  
					  }
				  if(d==5)return true;
				  if(d==4&&i==0&&select_game[d1][j]==0) return true;
				  if(d==4&&d1==c_max&&select_game[d1-5][j]==0)return true;
				  if(d==4&&d1>0&&d1<c_max&&select_game[d1][j]==0&&select_game[d1-5][j]==0) return true;
			  }
		  }
			  
			
	  }
	  //Kiem tra an doc
	  for(int i=c_min;i<c_max;i++){
		  
	  d=0;
	  
		  for(int j=r_min;j<r_max;j++)
		  {
			  if(select_game[i][j]==k)
			  {
				  d=1;
				  d1=-1;
				  for(int j1=j+1;j1<c_max;j1++)
					  if(select_game[i][j1]==k){
						  d++;
						  j=j1+1;
						  d1=j1+1;
					  }
					  else {
						  
						  break;
						  
					  }
				  if(d>=5)return true;
				  if(d==4&&j==0&&select_game[i][d1]==0) return true;
				  if(d==4&&d1==r_max&&select_game[i][d1-5]==0)return true;
				  if(d==4&&d1>0&&d1<r_max&&select_game[i][d1]==0&&select_game[i][d1-5]==0) return true;
			  }
		  }
			  
			
	  }
	  //kiem tra an cheo trai
	  for(int j=4;j<r_max;j++){
		  
		  d=0;
		  if(j<c_max)
			  for(int i=c_min;i<=j;i++)
			  {
				  if(select_game[i][j-i]==k)
				  {
					  
					  d=1;
					  d1=-1;
					  for(int i1=i+1;i1<=j;i1++)
						  if(select_game[i1][j-i1]==k){
							  d++;
							  d1=i1+1;
						  }
						  else {
							  
							  i=i1;
							  break;
							  
						  }
					  
				
					  
					  if(d>=5)return true;
					 // if(d==4&&i==c_min&&select_game[d1][j-d1]==0) return true;
					  
					 // if(d==4&&d1==j&&select_game[d1-5][j]==0)return true;
					  if(d==4&&d1>=5&&d1<j&&select_game[d1][j-d1]==0&&select_game[d1-5][j]==0) return true;
				  }
				  
			  }
				  
		  if(j>=c_max)
			  for(int i=c_min;i<c_max;i++)
			  {
				  if(select_game[i][j-i]==k)
				  {
					  
					  d=1;
					  d1=-1;
					  for(int i1=i+1;i1<=j;i1++)
						  if(select_game[i1][j-i1]==k){
							  d++;
							  d1=i1+1;
						  }
						  else {
							  d1=i1;
							  i=i1;
							  break;
							  
						  }
					  
				
					  
					  if(d>=5){
						  
					  System.out.printf(" 1 ");
					  return true;
					  }
					  // if(d==4&&i==c_min&&select_game[d1][j-d1]==0) return true;
					  
					 // if(d==4&&d1==j&&select_game[d1-5][j]==0)return true;
	 if(d==4&&d1>=5&&d1<j&&select_game[d1][j-d1]==0&&select_game[d1-5][j-(d1-5)]==0) {
		 System.out.printf(" 2 ");
		 return true;
	 }
				  }
			  }
				  
				
		  }
	  //kiem tra an cheo phai
for(int j=4;j<r_max;j++){
		  
		  d=0;
		  if(j<c_max)
			  for(int i=c_max-j-1;i<c_max;i++)
			  {
				  if(select_game[i][i-c_max+j+1]==k)
				  {
					  
					  d=1;
					  d1=-1;
					  for(int i1=i+1;i1<c_max;i1++)
						  if(select_game[i1][i1-c_max+j+1]==k){
							  d++;
							  
						  }
						  else {
							  d1=i1;
							  i=i1;
							  break;
							  
						  }
					  
				
					  
					  if(d>=5)return true;
					 // if(d==4&&i==c_min&&select_game[d1][j-d1]==0) return true;
					  
					 // if(d==4&&d1==j&&select_game[d1-5][j]==0)return true;
					  if(d==4&&d1>=5&&select_game[d1][d1-c_max+j+1]==0&&select_game[d1-5][d1-c_max+j+1-5]==0) return true;
				  }
				  
				  
			  }
				  
		  if(j>=c_max)
			  for(int i=c_min;i<c_max;i++)
			  {
				  if(select_game[i][i-c_max+j]==k)
				  {
					  
					  d=1;
					  d1=-1;
					  for(int i1=i+1;i1<c_max;i1++)
						  if(select_game[i1][i1-c_max+j]==k){
							  d++;	  
						  }
						  else {
							  d1=i1;
							  i=i1;
							  break;
							  
						  }
					  
					  if(d>=5){
						  System.out.printf(" 3 ");
						  return true;
					  }
					 // if(d==4&&i==c_min&&select_game[d1][j-d1]==0) return true;
					  
					 // if(d==4&&d1==j&&select_game[d1-5][j]==0)return true;
					  if(d==4&&d1>=5&&select_game[d1][d1-c_max+j]==0&&select_game[d1-5][d1-c_max+j-5]==0){
						  System.out.printf(" 4 ");
						  return true;
					  }
				  }  

			  }
				  
				
		  }
	  return false;
  }
  @Override
  protected void onDraw(Canvas canvas) {
	  
	  if(audio)
		  audio_on=BitmapFactory.decodeResource(getResources(), R.drawable.sound_on);
	  else 
		  audio_on=BitmapFactory.decodeResource(getResources(), R.drawable.sound_off);
  for(int i=c_min;i<c_max;i++)
	  for(int j=r_min;j<r_max;j++)
		  canvas.drawBitmap(quan_game[i][j], (i-c_min)*36, (j-r_min)*36, paint);
  canvas.drawBitmap(back,50,740, paint);
  canvas.drawBitmap(audio_on,140,740,paint);
  canvas.drawBitmap(danhlai,250,740,paint);
  if(runing==false)
	  canvas.drawBitmap(win_lose,50,50,paint);

  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {

	 
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    	if(event.getX()>=250&&event.getX()<=250+111&&event.getY()>=740&&event.getY()<=740+50){
   		 for(int i=c_min;i<c_max;i++)
   			   for(int j=r_min;j<r_max;j++){
   		    quan_game[i][j]=BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_emty);
   			   select_game[i][j]=0;
   			   }
   		 x_1=-1;
   		 y_1=-1;
   		  x_attack=-1;
   		    y_attack=-1;
   		    eval_attack=0;
   		    runing=true;
   		    runing_=true;
   		    win_lose=null;
   		 }
    	 if(runing){
    		 
    		 if(event.getX()>=50&&event.getX()<=50+59&&event.getY()>=740&&event.getY()<=740+55)
    			 if(x_1!=-1&&y_1!=-1){
    				 quan_game[x_1][y_1]=BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_emty);
    				 quan_game[x_attack][y_attack]=BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_emty);
    				 select_game[x_1][y_1]=0;
    				 select_game[x_attack][y_attack]=0;
    				 
    			 }
    		 if(event.getX()>=140&&event.getX()<=140+69&&event.getY()>=740&&event.getY()<=740+57)
    			 if(audio)
    				 audio=false;
    			 else audio=true;
    	////	 if(audio)
    		///	  audio_back_ground.start();
    		//  else
    		//	  audio_back_ground.stop();
    	int x1=(int)event.getX()/36;
    	int y1=(int )event.getY()/36;
    	if(x1>=c_min&&x1<c_max&&y1>=r_min&&y1<r_max&&select_game[x1][y1]==0){
    		x_1=x1;
    		y_1=y1;
    		
    	quan_game[x1][y1]=BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_selected);
    	select_game[x1][y1]=-1;
    	if(audio)
    		audio_x.start();
    	 if(check_win(-1)){
    		  System.out.println("Ban da thang");
    		 win_lose=BitmapFactory.decodeResource(getResources(), R.drawable.go_win);
    	 runing=false;
    	 if(audio)
    		 audio_win.start();
    	 }
    	 if(runing){
    		  danh();
    		  quan_game[x_attack][y_attack]=BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_select_1);
    	select_game[x_attack][y_attack]=1;
    	if(audio)
    	audio_o.start();
    	if(check_win(1)){
    		System.out.print("Ban da thua ");
    		runing=false;
    		win_lose=BitmapFactory.decodeResource(getResources(), R.drawable.go_lose);
    		//runing_=false;
    	if(audio)
    		audio_lose.start();
    	}
 
    	 }
    	}
    	 }
    	 //if(runing_==false)
    		// System.out.println("Ban da thua ");
      return true;
    case MotionEvent.ACTION_MOVE:

      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
    	
      break;
    default:
      return false;
    }
    // Schedules a repaint.
    invalidate();
	  
    
    return true;
    
  
  }
  
} 
